package org.bookie.web.pages.account;

import java.util.List;

import org.apache.tapestry.annotations.InjectComponent;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.bookie.framework.CommonBasePage;
import org.bookie.framework.components.PeriodComponent;
import org.bookie.model.Role;
import org.bookie.model.Transaction;
import org.bookie.model.User;
import org.bookie.model.security.Access;
import org.bookie.model.services.TransactionDAO;
import org.bookie.model.services.UserDAO;

@Access(role=Role.USER)
public abstract class TransactionList extends CommonBasePage implements PageBeginRenderListener{
	/**Identifier of the client component containing the period component.*/
	public static final String PERIOD = "period";
	
	/**
	 * @see PageBeginRenderListener#pageBeginRender(org.apache.tapestry.event.PageEvent)
	 */
	public void pageBeginRender(PageEvent event) {
		User user = getUserDAO().findByEmail(
				getAuthenticationContext().getUser().getEmail());
		List transactions = getTransactionDAO().findByUser(
								user, getPeriodComponent().getPeriod());
		setTransactions(transactions);
	}
	
	/**
	 * Injected User service.
	 * @return instance of UserDAO
	 */
	@InjectObject("service:bookie.model.UserDAO")
	public abstract UserDAO getUserDAO();
	
	/**
	 * Injected Transaction service.
	 * @return instance of TransactionDAO
	 */
	@InjectObject("service:bookie.model.TransactionDAO")
	public abstract TransactionDAO getTransactionDAO();
	
	/**
	 * Injected {@link PeriodComponent}s
	 * @return instance of PeriodComponent
	 */
	@InjectComponent(TransactionList.PERIOD)
	public abstract PeriodComponent getPeriodComponent();
	/**
	 * Get the {@link Transaction}s of the authenticated user.
	 * @return a list of Transactions
	 */
	public abstract List getTransactions();
	
	/**
	 * Set the {@link Transaction}s of the authenticated user.
	 * @param transactions transactions to set
	 */
	public abstract void setTransactions(List transactions);
	
	/**
	 * Set the current transaction of the iteration over all {@link Transaction}s
	 * @param Transaction current transaction to set
	 */
	public abstract void setCurrentTransaction(Transaction transaction);

}
